﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Entity.Core;

namespace CreateEntity
{
    public partial class AddAccess : Form
    {
        DataBaseTree dbTree;
        public AddAccess(DataBaseTree dbTree)
        {
            this.dbTree = dbTree;
            InitializeComponent();
        }

        //保存
        private void btnOK_Click(object sender, EventArgs e)
        {
            ConnectionModel model = new ConnectionModel()
                {
                    ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Database Password={1};User Id={2}", txtDataBase.Text,txbPwd.Text,txbUserName.Text),
                    
                    DataBaseName = Path.GetFileName(txtDataBase.Text),
                    Provider = "System.Data.OleDb"
                };
            Connections conn = new Connections();
            if (conn.Add(model))
            {
                dbTree.LoadDataBase();
                this.Close();
            }
            else
            {
                MessageBox.Show("保存失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
        }

        private void AddAccess_Load(object sender, EventArgs e)
        {

        }

        //浏览按钮
        private void btnDataBase_Click(object sender, EventArgs e)
        {
            if (openDataBase.ShowDialog().Equals(DialogResult.OK))
            {
                this.txtDataBase.Text = openDataBase.FileName;
            }
        }

        //测试按钮
        private void btnTest_Click(object sender, EventArgs e)
        {
            ConnectionModel model = new ConnectionModel()
            {
                ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Database Password={1};User Id={2}", txtDataBase.Text, txbPwd.Text, txbUserName.Text),

                DataBaseName = Path.GetFileName(txtDataBase.Text),
                Provider = "System.Data.OleDb"
            };
            Connections conn = new Connections();
            if (conn.CheckConnection(model))
            {
                MessageBox.Show("连接成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
            else
            {
                MessageBox.Show("连接失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
        }
    }
}
